import type { PlasmoMessaging } from '@plasmohq/messaging'
import someCoolImage from "data-base64:~assets/icon.png"
import { Storage } from "@plasmohq/storage";
const storage=new Storage()
const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
  const { duration } = req.body; // 获取前端传来的倒计时用户设置的时长
  console.log("倒计时时长:", duration);

  chrome.windows.getCurrent().then( function (window) {
    console.log("倒计时开始");

    let remainingSeconds = duration; // 倒计时剩余时间，初始值为用户设置的时长

    const countdownInterval =  setInterval(function(){

      remainingSeconds--;
      this.storedTimer = remainingSeconds;
      storage.set("storedTimer", this.storedTimer+"_timer");

      if (remainingSeconds === 0) {
        // 倒计时结束时的逻辑
        console.log("倒计时结束");

        clearInterval(countdownInterval);

        // res.send("111")
        //创建一个通知面板
        storage.set('storedTimer','0').then(()=>{
          chrome.notifications.create('', {
            type: 'basic',
            iconUrl: someCoolImage,
            title: '倒计时已结束',
            message: '倒计时已结束，时间到了！'
          });

        })
      } else {
        // 每秒的倒计时逻辑
        // console.log("倒计时剩余时间:", remainingSeconds);
      }
    }, 1000); // 每秒更新一次倒计时
  });
}

export default handler;